﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AudioManager : BaseManager
{

    private AudioSource _bgmAudioSource;

    private AudioSource _soundAudioSource;
   
    /// <summary>
    /// 音效字典
    /// </summary>
    private Dictionary<SoundType, AudioClip> _soundDict;
    public override void OnInit()
    {
        GameObject audioManagerGo = new GameObject("AudioManager");
        GameObject bgm = new GameObject("BGM");
        GameObject sound = new GameObject("Sound");
        bgm.transform.SetParent(audioManagerGo.transform);
        sound.transform.SetParent(audioManagerGo.transform);
        _bgmAudioSource = bgm.AddComponent<AudioSource>();
        _soundAudioSource = sound.AddComponent<AudioSource>();
        //音源组件的初始设置
        _bgmAudioSource.loop = true;
        _bgmAudioSource.playOnAwake = true;
        _soundAudioSource.loop = false;
        _soundAudioSource.playOnAwake = false;

        _bgmAudioSource.volume = 0.1f;// 加载声音资源
        LoadAudioResources();
    }
    /// <summary>
    /// 加载声音资源
    /// </summary>
    private void LoadAudioResources()
    {
        
        //音效音乐资源的加载
//        AudioClip awardClip = Resources.Load<AudioClip>("Sounds/Send");
//        AudioClip brickClip =Resources.Load<AudioClip>("Sounds/Receive");
        _soundDict = new Dictionary<SoundType, AudioClip>();
//        _soundDict.Add(SoundType.Send,awardClip);
//        _soundDict.Add(SoundType.Receive,brickClip);
        
       
    }

   

    /// <summary>
    /// 播放音效
    /// </summary>
    /// <param name="soundType"></param>
    public void PlaySound(SoundType soundType)
    {
        _soundAudioSource.Stop();
        _soundAudioSource.clip = _soundDict[soundType];
        _soundAudioSource.Play();
    }


    /// <summary>
    /// 音量控制
    /// </summary>
    /// <param name="bgmVolume"></param>
    /// <param name="soundVolume"></param>
    public void ControlVolume(float bgmVolume,float soundVolume)
    {
        _bgmAudioSource.volume = bgmVolume;
        _soundAudioSource.volume = soundVolume;
    }
    
    public override void OnUpdate()
    {
    }

    public override void OnDestroy()
    {
    }
}


public enum SoundType
{
    Send,Receive
}